package io.seata.samples.tcc.springboot.springcloud.controller;

import io.seata.samples.tcc.springboot.springcloud.service.TccTransactionService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 发起事务控制器
 *
 * @author louis
 */
@RestController
public class TccController {

    @Resource
    private TccTransactionService tccTransactionService;

    /**
     * 分布式事务提交示例接口
     */
    @GetMapping("/testCommit")
    public String testCommit() {
        String result = tccTransactionService.testCommit();
        return "----------test transaction commit---------- \n" + result;
    }

    /**
     * 分布式事务回滚示例接口
     */
    @GetMapping("/testRollback")
    public String testRollback() {
        String result = "";
        try {
            tccTransactionService.testRollback();
        } catch (Throwable t) {
            result = t.getMessage();
        }
        return  "----------test transaction rollback ---------- \n" + result;
    }

}
